package org.xx.armory.swing.controllers;

import org.xx.armory.swing.components.InternalFramePane;

import javax.swing.*;
import java.awt.Image;
import java.beans.PropertyChangeListener;

public class InternalFrameUIContainerPropertyChangeAdapter
        extends PaneUIContainerPropertyChangeAdapter<InternalFramePane, JInternalFrame>
        implements PropertyChangeListener {

    /**
     * {@inheritDoc}
     */
    @Override
    public void propertyChange(
            InternalFramePane container,
            JInternalFrame owner,
            String propertyName,
            Object oldValue,
            Object newValue
    ) {
        super.propertyChange(container, owner, propertyName, oldValue, newValue);

        switch (propertyName) {
            case "title":
                setOwnerTitle(owner, container.getTitle());
                break;
            case "icon":
                setOwnerIcon(owner, container.getIcon());
                break;
            case "owner":
                setOwnerTitle(owner, container.getTitle());
                setOwnerIcon(owner, container.getIcon());

                owner.pack();
                break;
            default:
                break;
        }
    }

    private void setOwnerTitle(
            JInternalFrame owner,
            String title
    ) {
        if (owner != null) {
            owner.setTitle(title);
        }
    }

    private void setOwnerIcon(
            JInternalFrame owner,
            Image icon
    ) {
        if (owner != null) {
            owner.setFrameIcon(new ImageIcon(icon));
        }
    }
}

